﻿// 控件.cpp : 定义应用程序的入口点。
//

#include "pch.h"
#include "framework.h"
#include "控件.h"
#include <stdio.h>
#include <string.h>

#define MAX_LOADSTRING 100

#define HAVE_STATIC  0
#define HAVE_BUTTON  0
#define HAVE_TEST    0
#define HAVE_BOX     0
/*
控件：
    1.什么是控件：
        就是个 具备特殊功能的 （有标准模版） 子窗口
    2.创建控件就是创建窗口
        CreateWindow
        不需要自行注册窗口类
        必须具备如下风格：
            WS_CHILD    子窗口
            WS_VISIBLE  有效的
    3.基本控件
        3.1静态文本框    Static
            3.1.1   静态文本框
            3.1.2   静态位图框
        3.2按钮           Button      （按钮是最多的控件）
            3.2.1   普通
            3.2.2   单选
            3.2.3   复选框
            3.2.4   组合框
            3.2.5   三态复选框
        3.3文本编辑框        Edit
            
        3.4下拉列表框        ListBox

        3.5组合框              ComboBox


    4.
        4.1创建控件
            WM_CREATE
            CreateWindow
        4.2控制
            菜单
            WM_COMMAND消息
            SS_NOTIFY   注意：这个一定要加 不然不会产生点击效应


*/





// 全局变量:
HINSTANCE hInst;                                // 当前实例
WCHAR szTitle[MAX_LOADSTRING];                  // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING];            // 主窗口类名


void OnCreate(HWND hWnd);


// 此代码模块中包含的函数的前向声明:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此处放置代码。

    // 初始化全局字符串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MY, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 执行应用程序初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY));

    MSG msg;

    // 主消息循环:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函数: MyRegisterClass()
//
//  目标: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_MY);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函数: InitInstance(HINSTANCE, int)
//
//   目标: 保存实例句柄并创建主窗口
//
//   注释:
//
//        在此函数中，我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 将实例句柄存储在全局变量中

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目标: 处理主窗口的消息。
//
//  WM_COMMAND  - 处理应用程序菜单
//  WM_PAINT    - 绘制主窗口
//  WM_DESTROY  - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_CREATE:
        OnCreate(hWnd);


        break;
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析菜单选择:
            switch (wmId)
            {
#if HAVE_STATIC
            case 1001:
                {
                    
                    LRESULT ret;
                    //MessageBox(NULL, "点击了静态文本框", "Static ", NULL);
                    if (STN_CLICKED == HIWORD(wParam))                  //单击操作
                    {
                        //加载位图
                        //HICON hIcon1 = LoadIcon(hInst, MAKEINTRESOURCE(130));
                        HANDLE hIcon1 = LoadImage(hInst, MAKEINTRESOURCE(130), 0, 32, 32, LR_LOADTRANSPARENT);
                        //获取控件句柄
                       HWND hStaticItem = GetDlgItem(hWnd,1002);        //获取到对话框（弹出来的 窗口就是特殊的对话框）的成员

                        //操作控件  发送消息
                       ret = SendMessage(hStaticItem, STM_SETICON/*设置图标*/, (WPARAM)hIcon1/*附加信息 那个图标*/, NULL);

                    }
                    else if(STN_DBLCLK == HIWORD(wParam))               //双击操作s
                    {
                        //双击换成抽象绘图
                        HICON hIcon2 = LoadIcon(hInst, MAKEINTRESOURCE(130));
                        //HANDLE hIcon2 = LoadImage(hInst, MAKEINTRESOURCE(130), 0, 32, 32, LR_LOADTRANSPARENT);
                        //获取控件句柄
                        HWND hStaticItem = GetDlgItem(hWnd, 1002);
                        //操作控件 发送对应消息
                        ret = SendMessage(hStaticItem, STM_SETICON/*设置图标*/, (WPARAM)hIcon2/*附加信息 那个图标*/, NULL);
                    }

                }
            break;
#endif
#if HAVE_BOX
            case 2001:
            {
                //获取组合框消息
                char buff[256] = { 0 };
                DlgDirSelectComboBoxEx(hWnd, buff, 255, 6001);
                MessageBox(NULL, buff, "ComboBox", NULL);
            }
            break;
#endif
#if HAVE_BUTTON
            case 2001:
                {
                    //MessageBox(NULL, "点击了确定按钮", "Button ", NULL);

                    //获取控件的句柄
                    HWND hRadioA = GetDlgItem(hWnd, 2002);
                    HWND hRadioB = GetDlgItem(hWnd, 2003);
                    HWND hRadioC = GetDlgItem(hWnd, 2004);
                    HWND hRadioD = GetDlgItem(hWnd, 2005);

                    //SendMessage(hRadioA, BM_SETCHECK/*set*/, BST_CHECKED, NULL);
                     
                   int  ret =  SendMessage(hRadioA, BM_GETCHECK/*get*/, BST_CHECKED, NULL);
                   char buff[56] = { 0 };
                   sprintf(buff, "ret:%d", ret); 
                   MessageBox(NULL, buff, "Button ", NULL);


                }
                break;
#endif
#if HAVE_TEST
            case 2001:
            {
                HWND hEdit1 = GetDlgItem(hWnd, 4001);
                HWND hEdit2 = GetDlgItem(hWnd, 4002);

                char username[56] = { 0 };
                char password[56] = { 0 };


                SendMessage(hEdit2, WM_GETTEXT, 55, (LPARAM)password);
                MessageBox(NULL, password, "密码", NULL);

                SendMessage(hEdit2, WM_SETTEXT, 55, (LPARAM)password);
            }
                break;
#endif

            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
            break;

        }
        break;

    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此处添加使用 hdc 的任何绘图代码...

            OnCreate(hWnd);



            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

void OnCreate(HWND hWnd)
{
#if HAVE_STATIC
    //1.静态框 Static
    CreateWindow("Static","富贵",WS_CHILD | WS_VISIBLE | SS_CENTER/*水平居中*/ | SS_NOTIFY/*  注意：这个一定要加 不然不会产生点击效应*/, 
        30, 30, 200, 30, hWnd, (HMENU)1001/*id*/, hInst, NULL);


    //静态位图框
    CreateWindow("Static", "#130", WS_CHILD | WS_VISIBLE | SS_ICON,
        30, 70, 32, 32, hWnd, (HMENU)1002/*id*/, hInst, NULL);


#endif
#if HAVE_BUTTON
    //按钮
    CreateWindow("Button", "点击确定", WS_CHILD | WS_VISIBLE,
        30, 110, 100, 30, hWnd, (HMENU)2001/*id*/, hInst, NULL);

    //单选框   这些默认就是一组的，所以自能单选
    CreateWindow("Button", "A", WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,
        30, 150, 40, 30, hWnd, (HMENU)2002/*id*/, hInst, NULL);
    CreateWindow("Button", "B", WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,
        80, 150, 40, 30, hWnd, (HMENU)2003/*id*/, hInst, NULL);
    CreateWindow("Button", "C", WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,
        130, 150, 40, 30, hWnd, (HMENU)2004/*id*/, hInst, NULL);
    CreateWindow("Button", "D", WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,
        180, 150, 40, 30, hWnd, (HMENU)2005/*id*/, hInst, NULL);


    //分组框   or组合框
    CreateWindow("Button", "组合", WS_CHILD | WS_VISIBLE | BS_GROUPBOX/*1*/,
        30, 180, 100, 100, hWnd, (HMENU)2011/*id*/, hInst, NULL);

    CreateWindow("Button", NULL, WS_CHILD | WS_VISIBLE | BS_GROUPBOX/*1*/,
        30, 300, 50, 50, hWnd, (HMENU)2011/*id*/, hInst, NULL);


    //复选框   多选按钮
    HDC hdc = GetDC(hWnd);
    TextOut(hdc, 500, 120, "复选框", 6);
    CreateWindow("Button", "AA", WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
        500, 150, 40, 30, hWnd, (HMENU)3002/*id*/, hInst, NULL);
    CreateWindow("Button", "BB", WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
        550, 150, 40, 30, hWnd, (HMENU)3003/*id*/, hInst, NULL);
    CreateWindow("Button", "CC", WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
        600, 150, 40, 30, hWnd, (HMENU)3004/*id*/, hInst, NULL);
    CreateWindow("Button", "DD", WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
        650, 150, 40, 30, hWnd, (HMENU)3005/*id*/, hInst, NULL);


    //三态复选框
    CreateWindow("Button", "3态复选框", WS_CHILD | WS_VISIBLE | BS_AUTO3STATE/*1*/,
        500, 200, 140, 30, hWnd, (HMENU)3006/*id*/, hInst, NULL);

    ReleaseDC(hWnd, hdc);
#endif

#if HAVE_TEST

    CreateWindow("Button", "登录", WS_CHILD | WS_VISIBLE,
        30, 110, 100, 30, hWnd, (HMENU)2001/*id*/, hInst, NULL);



    //文本编辑框 Edit
    CreateWindow("Edit", "nigger", WS_CHILD | WS_VISIBLE/*1*/,
        30, 30, 130, 30, hWnd, (HMENU)4001/*id*/, hInst, NULL);
    CreateWindow("Edit", "nigger", WS_CHILD | WS_VISIBLE | ES_PASSWORD/*1*/,
        30, 70, 130, 30, hWnd, (HMENU)4002/*id*/, hInst, NULL);


#endif

#if HAVE_BOX
    //按钮
    CreateWindow("Button", "Get", WS_CHILD | WS_VISIBLE,
        30, 30, 100, 30, hWnd, (HMENU)2001/*id*/, hInst, NULL);








    // 下拉列表框 ListBox
    HWND hListBox1 = CreateWindow("ListBox", "下拉列表", WS_CHILD | WS_VISIBLE | CBS_DROPDOWN/*1*/,
        100, 100, 100, 100, hWnd, (HMENU)5001/*id*/, hInst, NULL);

    //下拉列表不加东西 是看不见摸不着的     所以下面给下拉列表加些东西
    char buff[256] = { 0 };
    for (int i = 0; i < 7; i++)
    {
        sprintf(buff, "葫芦娃%d", i + 1);
        //操作控件就应该立刻想到SendMessage    
        SendMessage(hListBox1,LB_ADDSTRING/*添加字符串*/,(WPARAM)strlen(buff)/*类型可以不转换*/, (LPARAM)buff);
    }


    // 组合框 ComboBox
    HWND hComboBox1 = CreateWindow("ComboBox", "组合框", WS_CHILD | WS_VISIBLE | CBS_DROPDOWN/*1*/,
        300, 100, 100, 100, hWnd, (HMENU)6001/*id*/, hInst, NULL);

   
    for (int i = 0; i < 7; i++)
    {
        sprintf(buff, "hhhhhh%d", i + 1);
        //操作控件就应该立刻想到SendMessage    
        SendMessage(hComboBox1, CB_ADDSTRING/*添加字符串*/, (WPARAM)strlen(buff)/*类型可以不转换*/, (LPARAM)buff);
    }
#endif


}

